Claude/add minimizer filter method up8 al#670
Merged
alexander-akait merged 7 commits intomainfrom May 8, 2026
Merged
Conversation
Each built-in minimizer now exposes a `filter(name, info)` helper based on file extension. When `minify` is an array, each asset is dispatched only to the minimizers whose `filter` accepts it; chain semantics still apply when multiple minimizers claim the same asset. The `test` option defaults to `undefined` for the array form so that filters can decide. This lets a single `TerserPlugin` instance handle JS/CSS/HTML/JSON with one shared worker pool instead of forcing a separate plugin instance per type.
`optimize()` was branching on whether the user passed a single minimizer
or an array, both for the gate ("does anything accept this asset?") and
for per-asset slicing. Normalize implementations and options to parallel
arrays once at the top of `optimize()` and reuse a single
`matchingMinimizers(name, info)` helper for both checks. Pass arrays to
`minify.js` unconditionally — it already normalizes a single function to
a one-element array internally.
Also extract the four extension regexes (JS/JSON/HTML/CSS) into shared
constants in `src/utils.js` so the 15 built-in `filter` helpers don't
each repeat the same literal.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #670 +/- ##
==========================================
+ Coverage 95.87% 96.11% +0.23%
==========================================
Files 3 3
Lines 558 592 +34
Branches 199 201 +2
==========================================
+ Hits 535 569 +34
Misses 21 21
Partials 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…sset Keep the original `test` default (`/\.[cm]?js(\?.*)?$/i`) for both single- and array-of-minimizers form. Users who want to dispatch across asset types in one plugin instance widen `test` themselves — the README example now spells that out explicitly. In `optimize()`, fold the gate and the per-asset record into a single async pass: each asset returns either an empty array (skipped) or a one-element array containing the record (with the matched-implementation indices already computed). `Array.prototype.flat()` produces the final list. The per-asset task reads `matched` directly from the record so the filter regexes only run once per asset.
The per-asset shallow clone in `optimize()` only existed because `minify.js` mutated `module`/`ecma` on the caller's options object, which would have leaked across assets when a single options object was shared. Move that overlay inside the `minify.js` loop and build it as a fresh object via spread, so the same source options reference is safe to reuse. `optimize()` no longer pre-expands a `minimizerOptionsList`: when the configured options are an array, slice the matching subset (references, not clones); otherwise pass the single object straight through. With a shared options object and N minimizers across M assets that drops the cloning from N×M to a single overlay per minimizer call.
`Array.prototype.flat` only landed in Node 11; the engines field is `>= 10.13.0`. Restore the `.filter` + `.map` split and stash each asset's matched-minimizer indices in a `Map` keyed by name, so the matching regexes still run only once per asset and the array of records is built without `flat`.
terser 5.47.1 was published after the previous lockfile update; the CI test matrix on Node 10/12/14/16/18 uses `npm install` (which re-resolves to the latest within `^5.31.1`) so it pulled 5.47.1, while Node 20+ ran `npm ci` and stayed on 5.46.2. The two terser versions mangle the same input to slightly different identifiers, which left the committed snapshots only valid for one half of the matrix. Run `npm update terser` to align the lockfile with what the older-Node install path resolves to, then `jest -u` to refresh the affected snapshots in TerserPlugin/test-option/extractComments-option suites.
Drop the standalone `test/minimizer-filter.test.js` and re-express the same coverage as snapshot tests in `test/minify-option.test.js`, matching the surrounding style (real fixtures, `readsAssets` / `getErrors` / `getWarnings` snapshots). The four new cases cover: - a single TerserPlugin instance dispatching to terser + html-minifier via the array-of-minimizers form - a single minimizer whose `filter` returns false (asset stays as-is) - a single minimizer whose `filter` returns undefined (treated as accept) - an array of minimizers whose filters all reject the asset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
allow to use one instance of the plugin
What kind of change does this PR introduce?
feat
Did you add tests for your changes?
Yes
Does this PR introduce a breaking change?
No
If relevant, what needs to be documented once your changes are merged or what have you already documented?
Already done
Use of AI
Claude